

#import "NewsViewController.h"
#import "ItemView.h"
#import "Newsmodel.h"
#import "WXNetworkService.h"
#import "AblumViewController.h"
#import "WebViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface NewsViewController ()
{
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
    NSTimer *timer;
}
-(void)initheaderView;
//-(void)initContentView;
-(void)requestData;
-(void)refreshUI;
@end

@implementation NewsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
        self.title=@"新闻";
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self requestData];
}
-(void)loadView
{
    [super loadView];
    _Basetableview=[[UITableView alloc]initWithFrame:CGRectMake(0,0, kDeviceWidth, kDeviceHeight-49-64)];
    _Basetableview.backgroundColor=[UIColor clearColor];
    _Basetableview.rowHeight=60;
    _Basetableview.delegate=self;
    _Basetableview.dataSource=self;
    _Basetableview.hidden=YES;
    [self.view addSubview:_Basetableview];
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
    [_Basetableview deselectRowAtIndexPath:[_Basetableview indexPathForSelectedRow] animated:NO];
}
-(void)initheaderView
{
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, 166)];
    
    //滑动视图
    _scrollView=[[UIScrollView alloc]initWithFrame:headerView.bounds];
    _scrollView.pagingEnabled=YES;
    _scrollView.showsHorizontalScrollIndicator=NO;
    _scrollView.bounces=NO;
    _scrollView.delegate=self;
    [headerView addSubview:_scrollView];
    _scrollView.contentSize=CGSizeMake(_newsArray.count*kDeviceWidth, 166);
    //加载图片
    for (int i=0; i<_newsArray.count; i++)
    {
        Newsmodel *newsmodel=_newsArray[i];
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*kDeviceWidth, 0, kDeviceWidth, 166)];
        imageView.contentMode=UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled=YES;
        [imageView setImageWithURL:[NSURL URLWithString:newsmodel.imageURL]];
        [_scrollView addSubview:imageView];
        
        //label
        UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(i*kDeviceWidth, headerView.height-20, kDeviceWidth, 20)];
        title.text=newsmodel.title;
        title.font=[UIFont systemFontOfSize:16];
        title.textAlignment=NSTextAlignmentCenter;
        title.textColor=[UIColor whiteColor];
        title.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [_scrollView addSubview:title];
    }
    
    _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(130, 125, 60, 20)];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.numberOfPages=_newsArray.count;
    _pageControl.currentPage=0;
    _pageControl.tag=101;
    [headerView  addSubview:_pageControl];
    //添加一个定时器
    [self addTimer];
    _Basetableview.tableHeaderView=headerView;
}
-(void)requestData
{
    NSArray *data=[WXNetworkService newsData];
    _newsArray=[[NSMutableArray alloc]initWithCapacity:data.count];
    for (NSDictionary *Content in data) {
        Newsmodel *newsmodel=[[Newsmodel alloc]initWithContent:Content];
        [_newsArray addObject:newsmodel];
    }
    [self refreshUI];
}
-(void)addTimer
{
    timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
-(void)nextImage
{
    NSInteger page=0;
    if (_pageControl.currentPage==_newsArray.count-1)
    {
        page=0;
    }
    else
    {
        page=_pageControl.currentPage+1;
    }
    [_scrollView setContentOffset:CGPointMake(page*kDeviceWidth, 0) animated:YES];
}
-(void)removeTimer
{
    [timer invalidate];
    timer=nil;
}

-(void)refreshUI
{
    [self initheaderView];
    [_Basetableview reloadData];
    _Basetableview.hidden=NO;
}
#pragma mark滑动视图的协议方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self removeTimer];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int offset=_scrollView.contentOffset.x/kDeviceWidth;
    _pageControl.currentPage=offset;
}
#pragma DataSourse代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _newsArray.count-1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        //单元格布局
        //图片
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
        image.tag=2001;
        [cell.contentView addSubview:image];
        
        //标题
        UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(image.right+5, image.top,240, 25)];
        title.font=[UIFont boldSystemFontOfSize:16];
        title.tag=2002;
        [cell.contentView addSubview:title];
        
        //副标题
        UILabel *detail=[[UILabel alloc]initWithFrame:CGRectMake(image.right+5, title.bottom,240, 25)];
        detail.font=[UIFont systemFontOfSize:15];
        detail.textColor=[UIColor lightGrayColor];
        detail.tag=2003;
        [cell.contentView addSubview:detail];
        //类型
        UIImageView *type=[[UIImageView alloc]initWithFrame:CGRectMake(image.right+5, title.bottom, 14, 14)];
        type.hidden=YES;
        type.tag=2004;
        [cell.contentView addSubview:type];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    Newsmodel *newsmodel=_newsArray[indexPath.row+1];
    
    UIImageView *image=(UIImageView *)[cell.contentView viewWithTag:2001];
    UIImageView *typeview=(UIImageView *)[cell.contentView viewWithTag:2004];
    UILabel *title=(UILabel *)[cell.contentView viewWithTag:2002];
    UILabel *detail=(UILabel *)[cell.contentView viewWithTag:2003];

    
    
    title.text = newsmodel.title;
    detail.text = newsmodel.summary;
    [image setImageWithURL:[NSURL URLWithString:newsmodel.imageURL]];
    int type = [newsmodel.typeID intValue];
    if (type == kWordType)
    {
    }
    else if (type == kImageType) {
        
        typeview.hidden = NO;
        typeview.image = [UIImage imageNamed:@"sctpxw"];
        detail.left = typeview.right+5;
        
    }else {
        typeview.hidden = NO;
        typeview.image = [UIImage imageNamed:@"scspxw"];
        detail.left = typeview.right+5;
    }

//    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Newsmodel *newsModel=_newsArray[indexPath.row+1];
    int type=[newsModel.typeID intValue];
    if (type==kWordType) {
        WebViewController *web=[[WebViewController alloc]init];
        [self.navigationController pushViewController:web animated:YES];
    }
    else if (type==
             kImageType)
    {
        AblumViewController *ablum=[[AblumViewController alloc]init];
        [self.navigationController pushViewController:ablum animated:YES];
        
    }
    else
    {
        NSString *urlstring = @"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621112404690593.mp4";
        
        NSURL *url = [NSURL URLWithString:urlstring];
        
        MPMoviePlayerViewController *moviePlayer1 = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
        
        moviePlayer1.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
        moviePlayer1.moviePlayer.view.frame=CGRectMake(0, 200, 320, 200);;
        [self.view addSubview:moviePlayer1.view];
      
        [self presentMoviePlayerViewControllerAnimated:moviePlayer1 ];

    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
